//
//  WelcomeController.swift
//  Weibo
//
//  Created by mac on 16/1/15.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {

    override func loadView() {
        self.view = self.backImageView
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupUI()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidDisappear(animated)
        
        self.animation()
    }
    
    private func animation() {
        let offset: CGFloat = -UIScreen.mainScreen().bounds.size.height + 160
        
        self.headerImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offset)
        })
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                
                }) { (_) -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName("setingRootController", object: nil)
                }
        }
    
        
    }
    
    private func setupUI() {
        self.view.addSubview(headerImageView)
        self.view.addSubview(welcomeLabel)
        
        
        self.headerImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(90)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
        }
        
        self.welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.headerImageView.snp_bottom).offset(30)
            make.centerX.equalTo(self.headerImageView.snp_centerX)
        }
        
        
        self.headerImageView.layer.cornerRadius = 45
        self.headerImageView.layer.masksToBounds = true
        self.headerImageView.setImageWithURL(UserCountViewModel().headerImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    

    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var headerImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLabel: UILabel = UILabel(title: (UserCountViewModel().userName ?? "") + " 欢迎归来", color: UIColor.grayColor(), fontSize: 16)

}























